package com.cardpay.supplier;

import com.cardpay.common.db.IRowMapper;
import com.cardpay.common.db.JdbcTemplate;
import com.cardpay.common.util.SQLHelper;
import com.cardpay.page.PageBean;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by asusa on 2017/1/23.
 */
public class SupplierDao {

    private JdbcTemplate template = new JdbcTemplate();

    public List<Supplier> list(){
        final String sql = "SELECT * FROM t_supplier";
        return this.template.queryForList(sql, new SupplierRowMapper());
    }

    public Supplier findByBillId(String supplierId) {
        final String sql = "SELECT * from t_supplier INNER JOIN t_bill on t_supplier.id = t_bill.supplierId WHERE t_bill.id = ?";

        return this.template.query(sql, new SupplierRowMapper(), supplierId);
    }

    /**
     * 新增供应商
     *
     * @param supplier
     *            封装了待插入供应商信息的对象
     * @return 受影响的行数
     */
    public int add(Supplier supplier) {
        final String sql = "INSERT INTO t_supplier(id, name, contact, tel, fax, address,descriptions) VALUES(?, ?, ?, ?, ?, ?, ?)";

        Object[] params = new Object[] { //
                supplier.getId(), //
                supplier.getName(),//
                supplier.getContact(),//
                supplier.getTel(),//
                supplier.getFax(),//
                supplier.getAddress(),//
                supplier.getDescriptions()//
        };

        return this.template.update(sql, params);
    }

    public int delete(String id) {
        final String sql = "DELETE FROM t_supplier WHERE id = ?";

        return this.template.update(sql, id);
    }

    public int update(Supplier supplier) {
        final String sql = "UPDATE t_supplier SET name=?, contact=?, tel=?, fax=?, address=?, descriptions=? WHERE id=?";

        Object[] params = new Object[] { //
                supplier.getName(),//
                supplier.getContact(),//
                supplier.getTel(),//
                supplier.getFax(),//
                supplier.getAddress(),//
                supplier.getDescriptions(),//
                supplier.getId()
        };

        return this.template.update(sql, params);
    }

    /**
     * 根据供应商id查询供应商
     *
     * @param id
     *            供应商id
     * @return 供应商对象
     */
    public Supplier find(String id) {
        final String sql = "SELECT * FROM t_supplier WHERE id=?";

        return this.template.query(sql, new SupplierRowMapper(), id);
    }

    /**
     * 分页查询供应商列表（供应商列表及分页信息封装于PageBean中）
     *
     * @param pageNum
     *            当前页码
     * @param pageSize
     *            每页显示记录数
     * @param sqlHelper
     *            封装了分页查询语句以及查询条件的SQLHelper对象
     * @return 分页信息对象
     */
    public PageBean<Supplier> findPagingSupplier(int pageNum, int pageSize, SQLHelper sqlHelper) {
        return this.template.pagination(pageNum, pageSize, sqlHelper, new SupplierRowMapper());
    }

    public class SupplierRowMapper implements IRowMapper<Supplier>{
        @Override
        public Supplier mapRow(ResultSet rs) throws SQLException{
            Supplier supplier=new Supplier();
            supplier.setId(rs.getString("id"));
            supplier.setName(rs.getString("name"));
            supplier.setContact(rs.getString("contact"));
            supplier.setTel(rs.getString("tel"));
            supplier.setFax(rs.getString("fax"));
            supplier.setAddress(rs.getString("address"));
            supplier.setDescriptions(rs.getString("descriptions"));
            return supplier;
        }
    }
}
